package com.bookstore.mapper;

import com.bookstore.domain.User;
import org.apache.ibatis.annotations.*;

import java.util.List;
@Mapper
public interface UserMapper {
    @Select("<script>" +
            "select uid,name,username,sex,birthday,phone,email,uType,status from book_user" +
            "<where>" +
            "<if test=\"uType!=-1\"> uType=#{uType} </if>" +
            "<if test=\"name!=null\">and name like concat(\"%\",#{name},\"%\")</if>" +
            "</where>" +
            "</script>")
    List<User> findAll(@Param("uType") Integer uType, @Param("name")String name);
    //查询单个用户 根据uid
    @Select("select uid,name,username,sex,birthday,phone,email,code,status,uType from book_user where uid=#{uid}")
    User findByUid(Integer uid);

    //根据username和password查询
    @Select("select * from book_user where username=#{username} And password=#{password}")
    User findByUsernameAndPassword(User user);

    //激活账户
    @Update("update book_user set status=1 where code=#{code}")
    int updateStatusByCode(String code);

    //添加用户  注册
    @Insert("Insert into book_user (name,username,password,sex,birthday,phone,email,code,status,uType) values" +
            "(#{name},#{username},#{password},#{sex},#{birthday},#{phone},#{email},#{code},1,#{uType});")
    Integer addUser(User user);

    //修改密码
    @Update("update book_user set password=#{password} where username=#{username} And uid=#{uid}")
    Integer updatePassword(User user);


    //修改用户状态
    @Update("update book_user set status=#{status} where uid=#{uid}")
    int upStatusByUid(@Param("status") Integer status, @Param("uid")Integer uid);

    //根据uid删除用户
    @Delete("delete from book_user where uid=#{uid}")
    int delUser(@Param("uid") Integer uid);

    @Update("<script>" +
            "update book_user " +
            "<set>" +
            "username=#{username}," +
            "<if test=\"password!=null\">password=#{password},</if>" +
            "sex=#{sex},birthday=#{birthday},phone=#{phone},email=#{email},uType=#{uType}" +
            "</set>" +
            "where uid=#{uid}" +
            "</script>")
    int upUser(User user);

}
